package com.xjnt.base.support.admin.service;

import java.util.List;

import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.xjnt.base.support.admin.entity.Dictionary;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.frameworks.plugin.sql.SqlKit;

@Service
public class DictionaryService extends BaseService<Dictionary> {

	@Override
	public Dictionary getModel() {
		return Dictionary.me;
	}

	@Override
	public void clearCache() {
		CacheKit.removeAll("SysDict");
	}
	/**
	 * 通过字典目录编码获取字典数值列表
	 * @param code 字典目录编码
	 * @return List<Dictionary>
	 */
	public List<Dictionary> findByCode(final String code) {
		String cacheKey = getModel().getModelName() + ":" + code;
		List<Dictionary> lstDict = CacheKit.get("SysDict", cacheKey, new IDataLoader() {
			@Override
			public Object load() {
				return getModel().find(SqlKit.findSql("Dictionary.findByCode"), code);
			}
		});
		return lstDict;
	}

	/**
	 * 通过字典表类型获取目录或字典数值列表
	 * @param type 字典表类型
	 * @return List<Dictionary>
	 */
	public List<Dictionary> findByType(Integer type) {
		return getModel().find(SqlKit.findSql("Dictionary.findByType"), type);
	}
}
